package com.aauedu.springboot.controller;

import com.aauedu.springboot.bean.Admin;
import com.aauedu.springboot.bean.Monster;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@Slf4j
public class LoginController {


    @PostMapping({"/login"})
    public String login(Admin admin, HttpSession session, Model model, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.hasText(admin.getName()) && "123456".equals(admin.getPassword())) {
                session.setAttribute("loginAdmin", admin);
            //登录成功了，并且用户选择了“10天内免登录功能”。
            String f = request.getParameter("f");
            if ("1".equals(f)) {
                //创建cookie对象存储登录名和密码
                Cookie cookie1 = new Cookie("username", admin.getName());
                Cookie cookie2 = new Cookie("password", admin.getPassword());//真实情况下是加密的
                //设置Cookie的有效期10天
                cookie1.setMaxAge(60 * 60 * 24 * 10);
                cookie2.setMaxAge(60 * 60 * 24 * 10);
                //设置两个cookie的path（只要访问这个页面，浏览器就一定要携带这两个cookie）
                cookie1.setPath(request.getContextPath());
                cookie2.setPath(request.getContextPath());
                //响应cookie给浏览器
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }
            return "redirect:/monster.html";
        } else {
            model.addAttribute("msg", "用户名/密码错误");
            return "adminLogin";
        }
    }

    @GetMapping("/monster.html")
    public String monster(Model model, @SessionAttribute("loginAdmin")  Admin loginAdmin) {
        /*if (loginAdmin != null) {
            List<Monster> monsters = new ArrayList<>();
            monsters.add(new Monster(1, "牛魔王", "吹牛", 200, 5000, new Date()));
            monsters.add(new Monster(2, "白骨精", "善变", 100, 4000, new Date()));
            monsters.add(new Monster(3, "孙悟空", "72变", 500, 9000, new Date()));
            monsters.add(new Monster(4, "猪八戒", "吃东西", 300, 2000, new Date()));
            model.addAttribute("monsters", monsters);
            return "monster";
        } else {
            model.addAttribute("msg", "您还没有登录，请登录后重试~");
            return "adminLogin";
        }*/
        log.info("进入monster页面");
        List<Monster> monsters = new ArrayList<>();
        monsters.add(new Monster(1, "牛魔王", "吹牛", 200, 5000, new Date()));
        monsters.add(new Monster(2, "白骨精", "善变", 100, 4000, new Date()));
        monsters.add(new Monster(3, "孙悟空", "72变", 500, 9000, new Date()));
        monsters.add(new Monster(4, "猪八戒", "吃东西", 300, 2000, new Date()));
        model.addAttribute("monsters", monsters);
        return "monster";
    }
}
